在專案開發的過程中,常常會遇到這種情況:
這些其實都不是誰的錯,而是我們缺少一個「自動幫我們檢查」的機制。
👉 這就是 CI/CD 登場的時候:
它就像一個不會喊累的機器人,每次你 push 或發 PR,就自動幫你檢查、測試、甚至部署。
今天,我們就來用 GitHub Actions 簡單幫專案加上這個「自動守護者」,開啟 CI/CD 的第一步!
CI(Continuous Integration,持續整合)
每次 push 或 PR 時,自動跑測試、檢查程式碼,確保專案隨時健康。
好處:快速發現 bug、避免合併衝突、提升專案穩定度。
CD(Continuous Delivery / Deployment,持續交付/部署)
好處:讓產品更新流程更快、更穩、更少人為失誤。
GitHub Actions 是 GitHub 提供的 自動化工作流程服務,可以在 push、PR、發版 tag 等事件觸發時,執行你定義的工作,例如:
除了 GitHub Actions,常見的 CI/CD 工具有:
不是!
今天我們先聚焦在 CI 自動化檢查,讓專案更安全。
首先,在專案根目錄創建 .github/workflows
資料夾。
我們建立一個最簡單的 GitHub Actions workflow,包含以下檢查:
在剛剛建立的資料夾底下建立 ci.yml
檔案內容如下:
name: CI
# 觸發條件:當有程式碼推送到任何分支,或有 Pull Request 時
on:
push:
branches: [ "**" ]
pull_request:
branches: [ "**" ]
jobs:
# 代碼品質檢查工作
code-quality:
runs-on: ubuntu-latest
steps:
# 步驟 1: 檢出程式碼
- name: 📥 檢出程式碼
uses: actions/checkout@v4
# 步驟 2: 設置 Node.js 環境
- name: 📦 設置 Node.js
uses: actions/setup-node@v4
with:
node-version: '20.17.0'
cache: 'npm'
# 步驟 3: 安裝依賴
- name: 📚 安裝依賴套件
run: npm ci
# 步驟 4: 執行 Prettier 格式檢查
- name: 🎨 檢查程式碼格式
run: npm run format:check
# 步驟 5: 執行 ESLint 檢查
- name: 🔍 執行 ESLint 檢查
run: npm run lint
# 步驟 6: TypeScript 編譯檢查
- name: 🔨 TypeScript 編譯檢查
run: npx tsc --noEmit
on:
push:
branches: [ "**" ] # 任何分支有推送時觸發
pull_request:
branches: [ "**" ] # 任何 Pull Request 時觸發
runs-on: ubuntu-latest
→ 使用最新的 Ubuntu 環境node-version: '20.17.0'
→ 使用專案指定的 Node.js 版本# 1. 將 workflow 文件加入版本控制
git add .github/workflows/ci.yml
# 2. 提交變更
git commit -m "chore: add GitHub Actions CI workflow"
# 3. 推送到 GitHub
git push
前往你的 GitHub repository
點擊上方的 "Actions" 標籤
你會看到 workflow 正在執行或已完成
點擊任何一次執行可以查看詳細的執行記錄 (這邊可以發現程式碼格式上有些問題) → CI 檢查失敗
修復關於 migrations 資料夾底下程式碼格式問題 → 目標是讓 CI 能夠正確完整執行
推薦方法:讓 Prettier 和 ESLint 忽略 migrations 資料夾的檢查
這是最常見的做法,因為:
在 .prettierignore
加上
src/migrations/**/*.ts
修改 eslint.config.js
加上 ( ignores: ["src/migrations/**/*.ts"]
)
const js = require("@eslint/js");
const globals = require("globals");
const prettier = require("eslint-config-prettier");
const tsParser = require("@typescript-eslint/parser");
const tsPlugin = require("@typescript-eslint/eslint-plugin");
module.exports = [
{
// 新增:忽略 migrations 資料夾
ignores: ["src/migrations/**/*.ts"],
},
{
files: ["**/*.ts"], // 檢查 .ts 檔案
languageOptions: {
parser: tsParser, // 用 TypeScript 的解析器
sourceType: "module", // 支援 ES 模組
globals: {
...globals.node, // Node.js 的全域變數(像 process)
},
},
// ... existing code ...
},
];
進行 commit 與推送程式碼
commit : update ESLint/Prettier config to ignore migrations folder
一樣的方式查看 Github Action
結果 : 偵測出我們之前測試 ESLint 套件所建立的一個小檔案有問題 testLint.ts
移除 testLint.ts
後再 push 一次
結果 : 成功通過 CI 測試啦!
到這裡,你已經成功在專案中導入 第一個 GitHub Actions CI Workflow。
從現在開始,每次 push 或發 PR,GitHub 會幫你自動檢查程式碼,確保品質無虞。
如果只想在 main
和 develop
分支執行,可以改成:
on:
push:
branches: [ "main", "develop" ]
pull_request:
branches: [ "main", "develop" ]
到今天為止,我們已經幫專案加上了第一層保護網:
CI 就像一個安靜但可靠的守護者,幫助團隊維持專案品質。
chore: 🤖 add GitHub Actions CI workflow
chore: 🤖 update ESLint/Prettier config to ignore migrations